
import 'dart:async';
import 'package:easy_refresh/easy_refresh.dart';
import 'package:flutter/animation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:get/get.dart';
import 'package:lottie/lottie.dart';
import 'package:sptapp/api/interface/home_api.dart';
import 'package:sptapp/api/request/response_data.dart';
import 'package:sptapp/config/const.dart';
import 'package:sptapp/moudle/common_info.dart';
import 'package:sptapp/moudle/message.dart';
import 'package:sptapp/moudle/refresh/my_refresh.dart';
import 'package:sptapp/page/home/min_agree_dialog.dart';
import 'package:sptapp/widget/animation_img.dart';
import 'package:sptapp/widget/top_device_select.dart';
import '../../api/interface/user_api.dart';
import '../../moudle/event_bus.dart';
import '../../moudle/permiss_handler/permission_operate.dart';
String cndcValue = '';
String cndcUnit = '';
class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  State<HomePage> createState() => _HomePageState();
}

Map? grade;

class _HomePageState extends State<HomePage> with AutomaticKeepAliveClientMixin, TickerProviderStateMixin {

  String gfbValue = '';
  String gfbUnit = '';
  String dwValue = '';
  String dwUnit = '';
  String fzValue = '';
  String fzUnit = '';
  String dcsocValue = '0';
  String dcsocUnit = '';
  String rdynValue = '0';
  String rdynUnit = '';
  String dcdlValue = '-1';
  String moneyUnit = "\$";

  List _list = [];
  late StreamSubscription _subscription;
  late StreamSubscription _devicesubscription;
  late StreamSubscription _Slidersubscription;

  Timer? _timer;
  bool isopen = false;
  bool isData = false;
  bool isDone = false;

  _getUserGrade()async{
    ResponseData res = await UserAPI.getUserGrade();
    if(res.code == 200 && res.data != null){
      grade = res.data;
      List arr = grade!['invitationList'] ?? [];
      if(arr.isNotEmpty){
        _showAgreeDialog();
      }
    }
    if(mounted){
      setState(() {});
    }
  }

  _showAgreeDialog()async{
    SmartDialog.show(builder: (ctx){
      return MinAgreeDialog(
        sure: (){
          getDevicesList((){});
        },
        cancel: (){
          _getUserGrade();
        },
        user: grade!['invitationList'][0],
      );
    },alignment: Alignment.center,clickMaskDismiss: false);

  }



  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    print('initiate=================');
    _getUserGrade();
    _getList();
    getBaseUserInfo((){
      if(mounted){
        setState(() {});
      }
    });
    _subscription = eventBus.on<UserInfoChange>().listen((val){
      if(mounted){setState(() {});}
    });
    _devicesubscription = eventBus.on<DeviceInfoChange>().listen((val){
      _getList();
    });
    _Slidersubscription = eventBus.on<IndexSliderChange>().listen((val){
      setState(() {
        isopen = val.isOpen;
      });
    });

    _timer = Timer.periodic(const Duration(seconds: 2), (timer) {
      if(baseDeviceNum != null && baseDeviceNum != ''){
        eventBus.fire(IndexRefreshTimer());
        _getList();
      }
    });

  }

  _getList()async{
    getDevicesList((){
      isDone = true;
      if(baseDevices.isEmpty){
        // _showNoDeviceAlert();
        setState(() {
          gfbValue = '';
          gfbUnit = '';
          dwValue = '';
          dwUnit = '';
          fzValue = '';
          fzUnit = '';
          cndcValue = '';
          cndcUnit = '';
          dcsocValue = '0';
          dcsocUnit = '';
          rdynValue = '0';
          rdynUnit = '';
          dcdlValue = '-1';
          moneyUnit = "\$";

        });
      }else{
        if(baseDevice?['status'].toString() == '2'){
          if(mounted){
            setState(() {
              isData = false;
            });
          }
          return;
        }
        _getHomeData();
      }
    });
  }

  _getHomeData()async{
    ResponseData res = await HomeAPI.getHomeInfo(baseDeviceNum??'');
    if(res.code == 200 && res.data != null){
      isData = true;
      List arr = res.data['DataList'] ?? [];
      for (var item in arr) {
        if(item['code'] == 'BCP'){
          dcsocValue = item['value'] ?? '0';
          dcsocUnit = item['unit'] ?? '';
        }else if(item['code'] == 'D01_31'){
          rdynValue = item['value'] ?? '0';
          rdynUnit = item['unit'] ?? '';
        }else if(item['code'] == 'LPT'){
          fzValue = item['value'] ?? '0';
          fzUnit = item['unit'] ?? '';
        }else if(item['code'] == 'SP1'){
          gfbValue = item['value'] ?? '0';
          gfbUnit = item['unit'] ?? '';
        }else if(item['code'] == 'BCAP'){
          cndcValue = item['value'] ?? '0';
          cndcUnit = item['unit'] ?? '';
        }else if(item['code'] == 'ICT'){
          dwValue = item['value'] ?? '0';
          dwUnit = item['unit'] ?? '';
        }else if(item['code'] == 'BC'){
          dcdlValue = item['value'] ?? '-1';
        }
      }
      if(arr.isEmpty){
        gfbValue = '';
        gfbUnit = '';
        dwValue = '';
        dwUnit = '';
        fzValue = '';
        fzUnit = '';
        cndcValue = '';
        cndcUnit = '';
        dcsocValue = '0';
        dcsocUnit = '';
        rdynValue = '0';
        rdynUnit = '';
        dcdlValue = '-1';
        moneyUnit = "\$";
      }
    }

    if(mounted){
      setState(() {});
    }
  }

  _showNoDeviceAlert(){
    SmartDialog.show(builder: (ctx){
      return Container(
        height: 530.w,
        padding: EdgeInsets.only(left: 48.w,right: 48.w,top: 32.w,bottom: 48.w),
        decoration: BoxDecoration(
            color: Color(0xff1E1E1E),
            borderRadius: BorderRadius.only(topLeft: Radius.circular(32.w),topRight: Radius.circular(32.w))
        ),
        child: Column(
          children: [
            Container(
              child: Row(
                children: [
                  Text('Add new device',style: TextStyle(
                      color: Colors.white,
                      fontSize: 28.sp,
                      fontWeight: FontWeight.w600
                  ),),
                  GestureDetector(
                    onTap: (){
                      SmartDialog.dismiss();
                    },
                    child: Icon(Icons.close,size: 30.sp,color: Color(0xff999999),),
                  )
                ],
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
              ),
            ),
            SizedBox(height: 100.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                getCameraPermission(context, (){
                  Navigator.of(context).pushNamed('ScanPage').then((val){
                    if(mounted){setState(() {});}
                  });
                });
              },
              child: Container(
                height: 84.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: themeColor,
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Scan Your Device',style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
            SizedBox(height: 32.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                Navigator.of(context).pushNamed('MinAddDeviceInput').then((val){
                  if(mounted){setState(() {});}
                });
              },
              child: Container(
                height: 84.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: Color(0xff2A2A2A),
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Manual Entry'.tr,style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
          ],
        ),
      );
    },alignment: Alignment.bottomCenter);
    // SmartDialog.show(
    //     alignment: Alignment.center,
    //     builder: (BuildContext ctx){
    //       return Container(
    //         decoration: BoxDecoration(
    //           color: Colors.white,
    //           borderRadius: BorderRadius.circular(20.w)
    //         ),
    //         margin: EdgeInsets.symmetric(horizontal: 30.w),
    //         padding: EdgeInsets.symmetric(horizontal: 30.w,vertical: 30.w),
    //         child: Container(
    //           child: Column(
    //             mainAxisSize: MainAxisSize.min,
    //             children: [
    //               Container(
    //                 child: Text(alert_waring_title.tr),
    //               ),
    //               SizedBox(height: 30.w,),
    //               Container(
    //                 child: Text(home_have_no_device_alert_text.tr),
    //               ),
    //               SizedBox(height: 30.w,),
    //               Container(
    //                 child: Row(
    //                   mainAxisAlignment: MainAxisAlignment.spaceAround,
    //                   children: [
    //                     GestureDetector(
    //                       onTap: (){
    //                         SmartDialog.dismiss();
    //                       },
    //                       child: Container(
    //                         width: 200.w,
    //                         height: 90.w,
    //                         alignment: Alignment.center,
    //                         decoration: BoxDecoration(
    //                           color: Colors.grey,
    //                           borderRadius: BorderRadius.circular(20.w)
    //                         ),
    //                         child: Text(alert_waring_canclebtn_text.tr),
    //                       ),
    //                     ),
    //                     GestureDetector(
    //                       onTap: (){
    //                         SmartDialog.dismiss().then((val){
    //
    //                         });
    //                         // Navigator.of(context).pushNamed('AddDevicePage').then((val){
    //                         //   // _homeViewSHow();
    //                         //   _getList();
    //                         // });
    //                       },
    //                       child: Container(
    //                         width: 200.w,
    //                         height: 90.w,
    //                         alignment: Alignment.center,
    //                         decoration: BoxDecoration(
    //                             color: themeColor,
    //                             borderRadius: BorderRadius.circular(20.w)
    //                         ),
    //                         child: Text(alert_waring_surebtn_text.tr),
    //                       ),
    //                     )
    //                   ],
    //                 ),
    //               )
    //             ],
    //           ),
    //         ),
    //       );
    //     }
    // );
  }

  _showNoNetworkAlert(){
    SmartDialog.show(builder: (ctx){
      return Container(
        height: 530.w,
        padding: EdgeInsets.only(left: 48.w,right: 48.w,top: 32.w,bottom: 48.w),
        decoration: BoxDecoration(
            color: Color(0xff1E1E1E),
            borderRadius: BorderRadius.only(topLeft: Radius.circular(32.w),topRight: Radius.circular(32.w))
        ),
        child: Column(
          children: [
            Container(
              child: Row(
                children: [
                  Text('Device Network Settings',style: TextStyle(
                      color: Colors.white,
                      fontSize: 28.sp,
                      fontWeight: FontWeight.w600
                  ),),
                  GestureDetector(
                    onTap: (){
                      SmartDialog.dismiss();
                    },
                    child: Icon(Icons.close,size: 30.sp,color: Color(0xff999999),),
                  )
                ],
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
              ),
            ),
            SizedBox(height: 100.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                Navigator.of(context).pushNamed('DeviceNetPage').then((val){
                  if(mounted){setState(() {});}
                });
              },
              child: Container(
                height: 84.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: themeColor,
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Select network',style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
            SizedBox(height: 32.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                Navigator.of(context).pushNamed('DeviceNetPage').then((val){
                  if(mounted){setState(() {});}
                });
              },
              child: Container(
                height: 84.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: Color(0xff2A2A2A),
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Manual Entry'.tr,style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
          ],
        ),
      );
    },alignment: Alignment.bottomCenter);
    // SmartDialog.show(
    //     alignment: Alignment.center,
    //     builder: (BuildContext ctx){
    //       return Container(
    //         decoration: BoxDecoration(
    //             color: Colors.white,
    //             borderRadius: BorderRadius.circular(20.w)
    //         ),
    //         margin: EdgeInsets.symmetric(horizontal: 30.w),
    //         padding: EdgeInsets.symmetric(horizontal: 30.w,vertical: 30.w),
    //         child: Container(
    //           child: Column(
    //             mainAxisSize: MainAxisSize.min,
    //             children: [
    //               Container(
    //                 child: Text(alert_waring_title.tr),
    //               ),
    //               SizedBox(height: 30.w,),
    //               Container(
    //                 child: Text(home_have_no_network_alert_text.tr),
    //               ),
    //               SizedBox(height: 30.w,),
    //               Container(
    //                 child: Row(
    //                   mainAxisAlignment: MainAxisAlignment.spaceAround,
    //                   children: [
    //                     GestureDetector(
    //                       onTap: (){
    //                         SmartDialog.dismiss();
    //                       },
    //                       child: Container(
    //                         width: 200.w,
    //                         height: 90.w,
    //                         alignment: Alignment.center,
    //                         decoration: BoxDecoration(
    //                             color: Colors.grey,
    //                             borderRadius: BorderRadius.circular(20.w)
    //                         ),
    //                         child: Text(alert_waring_canclebtn_text.tr),
    //                       ),
    //                     ),
    //                     GestureDetector(
    //                       onTap: (){
    //                         SmartDialog.dismiss();
    //                         Navigator.of(context).pushNamed('DeviceNetPage').then((val){
    //                           // _homeViewSHow();
    //                           _getList();
    //                         });
    //                       },
    //                       child: Container(
    //                         width: 200.w,
    //                         height: 90.w,
    //                         alignment: Alignment.center,
    //                         decoration: BoxDecoration(
    //                             color: themeColor,
    //                             borderRadius: BorderRadius.circular(20.w)
    //                         ),
    //                         child: Text(alert_waring_surebtn_text.tr),
    //                       ),
    //                     )
    //                   ],
    //                 ),
    //               )
    //             ],
    //           ),
    //         ),
    //       );
    //     }
    // );
  }

  String _getImageUrl(int index){
    if(index == 0){
      if(gfbValue != null && gfbValue.isNotEmpty && double.parse(gfbValue) > 0){
        return 'assets/home/top/2024-11-5上_00';
      }
      return 'assets/home/home_solar_panel_new.png';
    }
    if(index == 1){
      if(dwValue != null && dwValue.isNotEmpty && double.parse(dwValue) > 0){
        return 'assets/home/right_bk/右_00';
      }
      if(dwValue != null && dwValue.isNotEmpty && double.parse(dwValue) < 0){
        if(gfbValue != null && gfbValue.isNotEmpty && double.parse(gfbValue) > 0){
          return 'assets/home/right/右_00';
        }
        return 'assets/home/right/右_00';
      }
      return 'assets/home/home_grid_new.png';
    }
    if(index == 2){
      if(cndcValue != null && cndcValue.isNotEmpty && double.parse(cndcValue) < 0){
        return 'assets/home/bottom_bk/2024-11-5下_00';
      }
      if(cndcValue != null && cndcValue.isNotEmpty && double.parse(cndcValue) > 0){
        if(gfbValue != null && gfbValue.isNotEmpty && double.parse(gfbValue) > 0){
          return 'assets/home/bottom/2024-11-5下_00';
        }
        return 'assets/home/bottom/2024-11-5下_00'; //变灰色
      }
      return 'assets/home/home_enegry_storage_new.png';
    }
    if(index == 3){
      if(fzValue != null && fzValue.isNotEmpty && double.parse(fzValue) > 0){
        if(gfbValue != null && gfbValue.isNotEmpty && double.parse(gfbValue) > 0){
          return 'assets/home/left/左_00';
        }
        return 'assets/home/left_bk/左_00';
      }
      return 'assets/home/home_out_power_new.png';
    }
    return '';
  }
  bool _getImageLoop(int index){
    bool isLoop = false;
    if(index == 0){
      if(gfbValue != null && gfbValue.isNotEmpty && double.parse(gfbValue) > 0){
        isLoop = true;
      }
    }
    if(index == 1){
      if(dwValue != null && dwValue.isNotEmpty && double.parse(dwValue) > 0){
        isLoop = true;
      }
      if(dwValue != null && dwValue.isNotEmpty && double.parse(dwValue) < 0){
        isLoop = true;
      }
    }
    if(index == 2){
      if(cndcValue != null && cndcValue.isNotEmpty && double.parse(cndcValue) < 0){
        isLoop = true;
      }
      if(cndcValue != null && cndcValue.isNotEmpty && double.parse(cndcValue) > 0){
        isLoop = true;
      }
    }
    if(index == 3){
      if(fzValue != null && fzValue.isNotEmpty && double.parse(fzValue) > 0){
        isLoop = true;
      }
    }
    return isLoop;
  }


  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    _subscription.cancel();
    _devicesubscription.cancel();
    _Slidersubscription.cancel();
    SmartDialog.dismiss();
    _timer?.cancel();
    grade = null;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        backgroundColor: defaultBlackBgColor,
        body: GestureDetector(
          onTap: (){
            // drawerKey.currentState?.closeSlider();
            // eventBus.fire(IndexSliderChange(false));
            // setState(() {
            //   isopen = false;
            // });
          },
          child: EasyRefresh(
            header: myRefreshHeader(),
            onRefresh: (){
              _getList();
            },
            child: isDone? Container(
              padding: EdgeInsets.zero,
              child:  baseDevices.isEmpty? _buildHomeNoData(1):
              baseDevice?['status'].toString() == '2'? _buildHomeNoData(2): SingleChildScrollView(
                child: Container(
                  width: 1.sw,
                  height:1.sh-(120.w+(ScreenUtil().bottomBarHeight>0?20.w:0)),
                  child:
                  Stack(
                    children: [
                      //背景图
                      Container(
                        margin: EdgeInsets.only(top: 150.h,),
                        width: 1.sw,
                        child: Image.asset('assets/home/home_new_house_bg.png',width: 1.sw,fit: BoxFit.fitWidth,),
                      ),
                      //顶部信息
                      Positioned(top: ScreenUtil().statusBarHeight+0.w,left: 0,right: 0,
                        child: _topWidget(),
                      ),
                      // Positioned(top: ScreenUtil().statusBarHeight+44,left: 0,right: 0,
                      //   child: Container(
                      //     padding: EdgeInsets.symmetric(horizontal: 30.w),
                      //     child: Row(
                      //       mainAxisAlignment: MainAxisAlignment.end,
                      //       children: [
                      //         Container(
                      //           child: Text(cndcValue.isNotEmpty && double.parse('${cndcValue ?? '0'}') <0?'Discharging': cndcValue.isNotEmpty && double.parse('${cndcValue ?? '0'}') >0?'Charging':'',style: TextStyle(
                      //               color:cndcValue.isNotEmpty && double.parse('${cndcValue ?? '0'}') >0? Color(0xff06df85) : themeColor,
                      //               fontSize: 30.sp,
                      //             fontWeight: FontWeight.w600
                      //           ),maxLines: 1,overflow: TextOverflow.ellipsis,),
                      //         ),
                      //         Expanded(child: Container()),
                      //         Container(
                      //           width: 16.w,
                      //           height: 16.w,
                      //           decoration: BoxDecoration(
                      //               color:baseDevice?['status'].toString() =='1'? Color(0xff06df85) : Color.fromRGBO(255, 255, 255, 0.5),
                      //             borderRadius: BorderRadius.circular(16.w)
                      //           ),
                      //         ),
                      //         SizedBox(width: 12.w,),
                      //         Container(
                      //           child: Text(baseDevice?['status'].toString() =='1'? 'Online':'Offline',style: TextStyle(
                      //               color:baseDevice?['status'].toString() =='1'? Color(0xff06df85) : Color.fromRGBO(255, 255, 255, 0.5),
                      //               fontSize: 30.sp,
                      //               fontWeight: FontWeight.w600
                      //           ),maxLines: 1,overflow: TextOverflow.ellipsis,),
                      //         ),
                      //       ],
                      //     ),
                      //   ),
                      // ),

                      //左侧信息
                      Positioned(
                          top: 820.h+62/2,
                          left: 0.w,
                          child: Container(
                            width: 400.w,
                            height: 330.w,
                            alignment: Alignment.centerRight,
                            child: Stack(
                              children: [
                                Positioned(right: 0,child: Container(
                                  child:  AnimationImg(
                                    counts: 149,
                                    imgWidth: 122.w,
                                    sufix: _getImageUrl(3),
                                    isLoop: _getImageLoop(3),
                                    imgUrl: _getImageUrl(3),
                                  ),//Image.asset(_getImageUrl(3),width: 61,),
                                ),),
                                Positioned(
                                  bottom: 0,
                                  right:160.w,
                                  child: GestureDetector(
                                    onTap: (){
                                      eventBus.fire(HomeDataClick(3));
                                    },
                                    child: Container(
                                      // color: Colors.red,
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Row(
                                            crossAxisAlignment: CrossAxisAlignment.end,
                                            mainAxisAlignment: MainAxisAlignment.start,
                                            children: [
                                              Container(
                                                child: Text('${fzValue.replaceAll("-", "")}',style: TextStyle(
                                                    color: whiteFontColor,
                                                    fontSize:40.sp,
                                                    fontWeight: FontWeight.w500,
                                                    height: 1
                                                ),maxLines: 1,overflow: TextOverflow.ellipsis,),
                                                constraints: BoxConstraints(
                                                    maxWidth: 90
                                                ),
                                                // color: Colors.green,
                                              ),
                                              SizedBox(width: 8.w,),
                                              Container(
                                                // color: Colors.green,
                                                // margin: EdgeInsets.only(bottom: 4.w),
                                                child: Text('${fzUnit}',style: TextStyle(
                                                    color: Color.fromRGBO(255, 255, 255, 0.8),
                                                    fontSize: 28.sp,
                                                    fontWeight: FontWeight.w400,
                                                    height: 1
                                                ),),
                                              )
                                            ],
                                          ),
                                          SizedBox(height: 6.w,),
                                          Text('${home_page_fz_text.tr}',style: TextStyle(
                                              color: Color.fromRGBO(255, 255, 255, 0.5),
                                              fontSize: 24.sp,
                                              fontWeight: FontWeight.w400
                                          ),)
                                        ],
                                      ),
                                    ),
                                  ),
                                )
                              ],
                            ),
                          )
                      ),

                      //右侧信息
                      Positioned(
                          top: 820.h+62/2 - 586.w + 18.w,
                          left: 380.w+45/2,
                          child: Container(
                            width: 350.w,
                            // color: Colors.green,
                            child: Stack(
                              children: [
                                Container(
                                  child: AnimationImg(
                                    counts: 149,
                                    imgWidth: 262.w,
                                    sufix: _getImageUrl(1),
                                    isLoop: _getImageLoop(1),
                                    imgUrl: _getImageUrl(1),
                                  )
                                ),
                                Positioned(
                                  right: 160.w,
                                  top: 4.w,
                                  child: GestureDetector(
                                    onTap: (){
                                      eventBus.fire(HomeDataClick(1));
                                    },
                                    child: Container(
                                      // color: Colors.red,
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Row(
                                            crossAxisAlignment: CrossAxisAlignment.end,
                                            children: [
                                              Text('${dwValue.replaceAll("-", "")}',style: TextStyle(
                                                  color: whiteFontColor,
                                                  fontSize:40.sp,
                                                  fontWeight: FontWeight.w500,
                                                  height: 1
                                              ),),
                                              SizedBox(width: 8.w,),
                                              Container(
                                                // margin: EdgeInsets.only(bottom: 4.w),
                                                child: Text('${dwUnit}',style: TextStyle(
                                                    color: Color.fromRGBO(255, 255, 255, 0.5),
                                                    fontSize: 28.sp,
                                                    fontWeight: FontWeight.w400,
                                                    height: 1
                                                ),),
                                              )
                                            ],
                                          ),
                                          SizedBox(height: 6.w,),
                                          Text(home_page_dw_text.tr,style: TextStyle(
                                              color: Color.fromRGBO(255, 255, 255, 0.5),
                                              fontSize: 24.sp,
                                              fontWeight: FontWeight.w400
                                          ),)
                                        ],
                                      ),
                                    ),
                                  ),
                                )
                              ],
                            ),
                          )
                      ),

                      //顶部信息
                      Positioned(
                          top: 830.h-584.w,
                          left: 370.w - 350.w+45/2,
                          child: Container(
                            width: 350.w,
                            height: 584.w,
                            // color: Colors.green,
                            alignment: Alignment.centerRight,
                            child: Stack(
                              children: [
                                Positioned(
                                    right: 80.w,
                                    top: 4.w,
                                    child: GestureDetector(
                                      onTap: (){
                                        eventBus.fire(HomeDataClick(0));
                                      },
                                      child: Container(
                                        // color: Colors.red,
                                        alignment: Alignment.centerRight,
                                        child: Column(
                                          crossAxisAlignment: CrossAxisAlignment.start,
                                          children: [
                                            Row(
                                              mainAxisAlignment: MainAxisAlignment.end,
                                              crossAxisAlignment: CrossAxisAlignment.end,
                                              children: [
                                                Text('${gfbValue.replaceAll("-", "")}',style: TextStyle(
                                                    color: whiteFontColor,
                                                    fontSize:40.sp,
                                                    fontWeight: FontWeight.w500,
                                                    height: 1
                                                ),),
                                                SizedBox(width: 8.w,),
                                                Container(
                                                  // margin: EdgeInsets.only(bottom: 4.w),
                                                  child: Text('${gfbUnit}',style: TextStyle(
                                                      color: Color.fromRGBO(255, 255, 255, 0.5),
                                                      fontSize: 28.sp,
                                                      fontWeight: FontWeight.w400,
                                                      height: 1
                                                  ),),
                                                )
                                              ],
                                            ),
                                            SizedBox(height: 6.w,),
                                            Text('${home_page_gfb_text.tr}',style: TextStyle(
                                                color: Color.fromRGBO(255, 255, 255, 0.5),
                                                fontSize: 24.sp,
                                                fontWeight: FontWeight.w400
                                            ),)
                                          ],
                                        ),
                                      ),
                                    ),
                                ),
                                Positioned(right: 0,child: Container(
                                  child: AnimationImg(
                                    counts: 99,
                                    imgHeight: 584.w,
                                    sufix: _getImageUrl(0),
                                    isLoop: _getImageLoop(0),
                                    imgUrl: _getImageUrl(0),
                                  )
                                  // Image.asset(_getImageUrl(0),height: 290,),
                                ),)
                              ],
                            ),
                          )
                      ),
                      //底部信息
                      Positioned(
                          top: 820.h+62 ,
                          left: 370.w+35/2-100.w,
                          child: Container(
                            width: 500.w,
                            height: 248.w,
                            // color: Colors.red,
                            child: Stack(
                              children: [
                                Positioned(left: 100.w,child: Container(
                                  child: AnimationImg(
                              counts: 99,
                              imgHeight: 248.w,
                              sufix: _getImageUrl(2),
                              isLoop: _getImageLoop(2),
                              imgUrl: _getImageUrl(2),
                            )
                                  // Image.asset(_getImageUrl(2),height: 131,fit: BoxFit.fitHeight,),
                                ),),
                                Positioned(
                                  bottom: 0,
                                  right: 200.w,
                                  child: GestureDetector(
                                    onTap: (){
                                      eventBus.fire(HomeDataClick(2));
                                    },
                                    child: Container(
                                      // width: 300.w,
                                      // color: Colors.green,
                                      alignment: Alignment.centerRight,
                                      child: Column(
                                        crossAxisAlignment: CrossAxisAlignment.start,
                                        children: [
                                          Container(
                                            child: Row(
                                              crossAxisAlignment: CrossAxisAlignment.end,
                                              mainAxisAlignment: MainAxisAlignment.end,
                                              children: [
                                                Text('${cndcValue.replaceAll("-", "")}',style: TextStyle(
                                                    color: whiteFontColor,
                                                    fontSize:40.sp,
                                                    fontWeight: FontWeight.w500,
                                                    height: 1
                                                ),),
                                                SizedBox(width: 8.w,),
                                                Container(
                                                  // margin: EdgeInsets.only(bottom: 4.w),
                                                  child: Text('${cndcUnit}',style: TextStyle(
                                                      color: Color.fromRGBO(255, 255, 255, 0.5),
                                                      fontSize: 28.sp,
                                                      fontWeight: FontWeight.w400,
                                                      height: 1
                                                  ),),
                                                ),
                                                Container(
                                                  child: Row(
                                                    children: [
                                                      SizedBox(width: 10.w,),
                                                      Visibility(visible: cndcValue.isNotEmpty, child: Container(
                                                        alignment: Alignment.bottomCenter,
                                                        height: 40.sp,
                                                        child: Column(
                                                          mainAxisAlignment: MainAxisAlignment.center,
                                                          children: [
                                                            SizedBox(height: 12.w,),
                                                            Container(
                                                              height: 6.w,
                                                              width: 6.w,
                                                              decoration: BoxDecoration(
                                                                  color: Colors.white,
                                                                  borderRadius: BorderRadius.circular(6.w)
                                                              ),
                                                            ),
                                                          ],
                                                        ),
                                                      )),
                                                      SizedBox(width: 10.w,),
                                                      Visibility(visible: cndcValue.isNotEmpty,child: Text('$dcsocValue$dcsocUnit',style: TextStyle(
                                                          color: whiteFontColor,
                                                          fontSize:40.sp,
                                                          fontWeight: FontWeight.w500,
                                                          height: 1
                                                      ),),),
                                                    ],
                                                  ),
                                                )
                                              ],
                                            ),
                                          ),
                                          SizedBox(height: 6.w,),
                                          Text('Battery'.tr,style: TextStyle(
                                              color: Color.fromRGBO(255, 255, 255, 0.5),
                                              fontSize: 24.sp,
                                              fontWeight: FontWeight.w400
                                          ),)
                                        ],
                                      ),
                                    ),
                                  ),
                                )
                              ],
                            ),
                          )
                      ),
                      //spt的logo
                      Positioned(
                        left: 370.w ,
                        top: 820.h,
                        child: Container(
                          child: Image.asset('assets/home/min_spt.png',width: 35,height: 62,fit: BoxFit.cover,),
                        ),
                      ),
                      //底部电池SOC
                      Positioned(
                          top: 1290.h,
                          child: Container(
                            width: 1.sw,
                            padding: EdgeInsets.symmetric(horizontal: 30.w),
                            child: Visibility(
                              visible: true,
                              child: Row(
                                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                                children: [
                                  ( rdynValue!=null && rdynValue.isNotEmpty && double.parse(rdynValue??'0')>0)?
                                  Container(
                                    height: 116.w,
                                    width: 316.w,
                                    padding: EdgeInsets.only(left: 40.w,right: 8.w),
                                    decoration: BoxDecoration(
                                        border: Border.all(color: Color.fromRGBO(255, 255, 255, 0.5),width: 2.w),
                                        borderRadius: BorderRadius.circular(8.w),
                                        color: Color.fromRGBO(255, 255, 255, 0.1)
                                    ),
                                    child: Column(
                                      mainAxisAlignment: MainAxisAlignment.center,
                                      crossAxisAlignment: CrossAxisAlignment.start,
                                      children: [
                                        // Image.asset('assets/home/home_scgl.png',width: 32.w,),
                                        // SizedBox(width: 20.w,),
                                        Container(
                                          child: Row(
                                            children: [
                                              Image.asset('assets/home/save_momney.png',width: 48.w,),
                                              SizedBox(width: 8.w,),
                                              Expanded(child: Text('\$$rdynValue',style: TextStyle(
                                                  color: whiteFontColor,
                                                  fontSize:28.sp,
                                                  fontWeight: FontWeight.w600
                                              ),maxLines: 1,overflow: TextOverflow.ellipsis,),
                                              )
                                            ],
                                          ),
                                        ),
                                        Text('Daily Saving'.tr,style: TextStyle(
                                            color: Color.fromRGBO(255, 255, 255, 0.2),
                                            fontSize: 20.sp,
                                            height: 2,
                                            fontWeight: FontWeight.w600
                                        ),)
                                      ],
                                    ),
                                  ):
                                  Container(
                                    height: 116.w,
                                    width: 316.w,
                                    padding: EdgeInsets.only(left: 40.w,right: 8.w),
                                    decoration: BoxDecoration(
                                        border: Border.all(color: Color.fromRGBO(255, 255, 255, 0.5),width: 2.w),
                                        borderRadius: BorderRadius.circular(8.w),
                                        color: Color.fromRGBO(255, 255, 255, 0.1)
                                    ),
                                    alignment: Alignment.center,
                                    child: Row(
                                      children: [
                                        // Image.asset('assets/home/home_scgl.png',width: 32.w,),
                                        // SizedBox(width: 20.w,),
                                        Container(
                                          child: Column(
                                            crossAxisAlignment: CrossAxisAlignment.start,
                                            mainAxisAlignment: MainAxisAlignment.center,
                                            children: [
                                              Row(
                                                crossAxisAlignment: CrossAxisAlignment.end,
                                                children: [
                                                  Image.asset('assets/home/home_load.png',width: 48.w,),
                                                  SizedBox(width: 8.w,),
                                                  Text(fzValue.isEmpty?'0':'${fzValue.replaceAll("-", "")}',style: TextStyle(
                                                      color: whiteFontColor,
                                                      fontSize:28.sp,
                                                      fontWeight: FontWeight.w600
                                                  ),),
                                                  SizedBox(width: 4.w,),
                                                  Container(
                                                    child: Text('${fzUnit}',style: TextStyle(
                                                        color: Color.fromRGBO(255, 255, 255, 0.8),
                                                        fontSize: 28.sp,
                                                        fontWeight: FontWeight.w600
                                                    ),),
                                                  )
                                                ],
                                              ),
                                              Text('${home_page_fz_text.tr}',style: TextStyle(
                                                  color: Colors.white,
                                                  fontSize: 20.sp,
                                                  height: 2,
                                                  fontWeight: FontWeight.w600,
                                              ),)
                                            ],
                                          ),
                                        )
                                      ],
                                    ),
                                  ),
                                  dcItem(),
                                ],
                              ),
                            ),
                          )
                      ),
                    ],
                  ),
                ),
              ),
            ):Container(),
          ),
        )
    );
  }

  Widget _buildHomeNoData(int type){
    String imgurl = '';
    String title = '';
    String content = '';
    String btnTitle='';
    if(type == 1){
      imgurl = 'assets/home/home_no_device.png';
      title = 'No device added'.tr;
      content = 'Add your device to get more information.'.tr;
      btnTitle = 'Add Device'.tr;
    }else if(type == 2){
      imgurl = 'assets/home/home_no_network.png';
      title = 'Network not Connected'.tr;
      content = 'Connect the internet to get more information.'.tr;
      btnTitle = 'Device Network Setting'.tr;
    }
    return Container(
      child: Column(
        children: [
          SizedBox(height: ScreenUtil().statusBarHeight+0.w,),
          _topWidget(),
          SizedBox(height: 240.h,),
          Image.asset(imgurl,width: 480.w,),
          SizedBox(height: 48.h,),
          Container(
            child: Text(title,style: TextStyle(
              color: Colors.white,
              fontSize: 32.sp,
            ),),
          ),
          SizedBox(height: 24.h,),
          Container(
            width: 448.w,
            child: Text(content,style: TextStyle(
              color: Color.fromRGBO(255, 255, 255, 0.5),
              fontSize: 28.sp,
            ),textAlign: TextAlign.center,),
          ),
          SizedBox(height: 60.h,),
          GestureDetector(
            onTap: (){
              if(type == 1){
                _showNoDeviceAlert();
              }else if(type == 2){
                Navigator.of(context).pushNamed('DeviceNetPage');
              }
            },
            child: Container(
              width: 690.w,
              height: 90.w,
              alignment: Alignment.center,
              decoration: BoxDecoration(
                color: themeColor,
                borderRadius: BorderRadius.circular(12.w)
              ),
              child: Text(btnTitle,style: TextStyle(
                color: Colors.white,
                fontSize: 32.sp
              ),),
            ),
          )
        ],
      ),
    );
  }

  Widget _topWidget(){
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 30.w),
      height: 44,
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.center,
        children: [
          GestureDetector(
            onTap: (){
              _showDeviceChoose();
              // eventBus.fire(IndexSliderChange(true));
              // setState(() {
              //   isopen = true;
              // });
              // if(drawerKey.currentState != null && drawerKey.currentState!.isDrawerOpen){
              //   drawerKey.currentState?.closeSlider();
              //   eventBus.fire(IndexSliderChange(false));
              //   setState(() {
              //     isopen = false;
              //   });
              // }else{
              // eventBus.fire(IndexSliderChange(true));
              // drawerKey.currentState?.openSlider();
              // setState(() {
              //   isopen = true;
              // });
              // }
            },
            child: Row(
              children: [
                // Container(
                //   child: Image.asset('assets/home/home_person.png',width: 64.w,),
                // ),
                // SizedBox(width: 20.w,),
                Visibility(visible: true,child: Container(
                  constraints: BoxConstraints(
                      maxWidth: 260.w
                  ),
                  child: Text(baseUserInfo['firstName']!=null? baseUserInfo['firstName']:baseUserInfo['nickname']??'user',style: TextStyle(
                      color: whiteFontColor,
                      fontSize: 40.sp,
                      fontWeight: FontWeight.w600
                  ),maxLines: 1,overflow: TextOverflow.ellipsis,),
                ),),
                SizedBox(width: 6.w,),
                Image.asset('assets/common/control_down.png',width: 48.w,)
              ],
            ),
          ),
          Visibility(child: Container(
            child: Text(cndcValue.isNotEmpty && double.parse('${cndcValue ?? '0'}') <0?'Discharging'.tr: cndcValue.isNotEmpty && double.parse('${cndcValue ?? '0'}') >0?'Charging'.tr:'Ready'.tr,style: TextStyle(
                color:cndcValue.isNotEmpty && double.parse('${cndcValue ?? '0'}') <0? themeColor : Color(0xff06df85),
                fontSize: 28.sp,
                fontWeight: FontWeight.w600
            ),maxLines: 1,overflow: TextOverflow.ellipsis,),
          ),visible: baseDevice != null && baseDevice?['status'].toString() != '2',),
          // Container(
          //   child: Image.asset('assets/home/home_logo.png',height: 40.w,),
          // )
          // GestureDetector(
          //   onTap: (){
          //     JhPickerTool.showStringPicker(context,
          //         data: _list,
          //         clickCallBack: _selectType,
          //         normalIndex: 0);
          //   },
          //   child: Row(
          //     children: [
          //       SizedBox(width: 20.w,),
          //       Text(home_page_change_device_text.tr,style: TextStyle(
          //           color: fontColor
          //       ),)
          //     ],
          //   ),
          // ),
        ],
      ),
    );
  }

  _showDeviceChoose(){
    return SmartDialog.show(builder: (ctx){
      return TopDeviceSelect(
        callBack: (type){
          _getList();
          if(type == 1){
            _showAddDeviceAlert();
          }
        },
      );
    },alignment: Alignment.topCenter,animationTime: Duration(milliseconds: 200));
  }

  _showAddDeviceAlert(){
    SmartDialog.show(builder: (ctx){
      return Container(
        height: 530.w,
        padding: EdgeInsets.only(left: 48.w,right: 48.w,top: 32.w,bottom: 48.w),
        decoration: BoxDecoration(
            color: Color(0xff1E1E1E),
            borderRadius: BorderRadius.only(topLeft: Radius.circular(32.w),topRight: Radius.circular(32.w))
        ),
        child: Column(
          children: [
            Container(
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('Add new device',style: TextStyle(
                      color: Colors.white,
                      fontSize: 28.sp,
                      fontWeight: FontWeight.w600
                  ),),
                  GestureDetector(
                    onTap: (){
                      SmartDialog.dismiss();
                    },
                    child: Icon(Icons.close,size: 30.sp,color: Color(0xff999999),),
                  )
                ],
              ),
            ),
            SizedBox(height: 100.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                getCameraPermission(context, (){
                  Navigator.of(context).pushNamed('ScanPage').then((val){
                    if(mounted){setState(() {});}
                  });
                });
              },
              child: Container(
                height: 108.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: themeColor,
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Scan Your Device',style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
            SizedBox(height: 32.w,),
            GestureDetector(
              onTap: (){
                SmartDialog.dismiss();
                Navigator.of(context).pushNamed('MinAddDeviceInput').then((val){
                  print('base===' + baseDevice.toString());
                  setState(() {});
                });
              },
              child: Container(
                height: 108.w,
                width: 654.w,
                alignment: Alignment.center,
                decoration: BoxDecoration(
                    color: Color(0xff2A2A2A),
                    borderRadius: BorderRadius.circular(8.w)
                ),
                child:Text('Manual Entry'.tr,style: TextStyle(
                    color: Colors.white,
                    fontSize: 28.sp,
                    fontWeight: FontWeight.w600
                ),),
              ),
            ),
          ],
        ),
      );
    },alignment: Alignment.bottomCenter);
  }

  Widget dcItem(){
    return Container(
      height: 116.w,
      width: 316.w,
      padding: EdgeInsets.all(6.w),
      decoration: BoxDecoration(
        border: Border.all(color: Color.fromRGBO(255, 255, 255, 0.5),width: 2.w),
        borderRadius: BorderRadius.circular(8.w),
        color: Color.fromRGBO(255, 255, 255, 0.1)
      ),
      child: Stack(
        children: [
          Container(
            decoration: BoxDecoration(
                color: cndcValue == null || cndcValue.isEmpty || double.parse('${cndcValue ?? '0'}') < 0? Colors.white : Color(0xff15FF43),
            ),
            width: double.parse(dcsocValue)  >= 100 ? 316.w : double.parse(dcsocValue)/100*316.w,
            height: 116.w,
          ),
          Container(
            width: 316.w,
            height: 116.w,
            child: Row(
              crossAxisAlignment: CrossAxisAlignment.center,
              children: [
                Visibility(child: Container(
                  margin: EdgeInsets.only(left: 28.w),
                  child: Image.asset('assets/home/home_scgl.png',width: 30.w,),
                ),visible:double.parse(dcdlValue??'-1') > 0,),
                SizedBox(width: 28.w,),
                Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text('${dcsocValue.replaceAll("-", "")}$dcsocUnit',style: TextStyle(
                      color: double.parse(dcsocValue) > 0?  Colors.black:Colors.white,
                      fontSize:28.sp,
                      fontWeight: FontWeight.w600,
                    ),),
                    Text(cndcValue.isNotEmpty && double.parse('${cndcValue ?? '0'}') <0?'Battery Discharging'.tr: cndcValue.isNotEmpty && double.parse('${cndcValue ?? '0'}') >0?'Battery Charging'.tr:'Battery Ready'.tr,style: TextStyle(
                        color: double.parse(dcsocValue) > 0?  Colors.black:Colors.white,
                        fontSize: 20.sp,
                        height: 1.8,
                        fontWeight: FontWeight.w600
                    ),)
                  ],
                )
              ],
            ),
          ),
        ],
      ),
    );
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
